import { Provide } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { Schema } from '../entity/schema';

@Provide()
export class SchemaService {
  @InjectEntityModel(Schema)
  schemaModel: Repository<Schema>;
  async save(schemaStr: string) {
    // create a entity object
    const schema = new Schema();
    schema.schema = schemaStr;

    // save entity
    const result = await this.schemaModel.save(schema);

    // save success
    return result;
  }
  async getLatestOne() {
    const result = await this.schemaModel.findOne({
      select: ['schema'],
      order: { id: 'DESC' },
    });
    return {
      errno: 0,
      data: result,
    };
  }
}
